/**
 * Created by zhangqingxi on 2017-03-31.
 */


/**
 * 加载表格
 *
 */
var lastIndex;
function  loadDataList() {

    $('#dataList').datagrid({
        url:modelPath+'findByParam.shtml',
        queryParams:{
            sort:'d.addTime',
            order:'desc'
        },
        // idField:'id',
        fitColumns:true,
        pagination:true,
        rownumbers:true,
        //singleSelect:true,
        sortOrder:true,
        striped:true,
        fit:true,
        columns:[[
            {field:'id',width:10,checkbox:true},

            {field:'monitorPointName',title:'检测点名称',width:10,formatter:function (value,row,index) {
                return '<a href="javascript:void(0)" onclick="showDetails(\''+row.id+'\')" style="color: blue">'+value+'</a>'

            }},
            {field:'monitorProjectName',title:'检测项目',width:10},
            {field:'monitorTime',title:'检测时间',width:10},
            {field:'sampleValue',title:'采样值',width:10},
            {field:'rectificationState',title:'下发状态',width:10,formatter:function (value,row,index) {
                if(value===null || row.delState===0){
                    return '<span class="color-important text-white" style="padding: 3px;border-radius:5px;">未下发</span>'
                }else {
                    if(value===1){
                        return '<span class="color-info text-white" style="padding: 3px;border-radius:5px;">整改中</span>'
                    }else {
                        return '<span class="color-important text-white" style="padding: 3px;border-radius:5px;">已整改</span>'
                    }
                }

            }},
            {field:'abnormalState',title:'异常状态',width:10,formatter:function (value,row,index) {
                if(value===0){
                    return '<span class="color-success text-white" style="padding: 3px;border-radius:5px;">不异常</span>'
                }else{
                    return '<span class="color-important text-white" style="padding: 3px;border-radius:5px;">异常</span>'
                }

            }},
            {field:'addUserName',title:'登记人',width:10},
            {field:'addDeptName',title:'登记部门',width:10},
            {field:'d.addTime',title:'登记时间',width:10,sortable:true,formatter:function (value,row,index) {
                return row.addTime;
            }}
        ]],
        toolbar: '#toolbar',
        view: detailview,
        detailFormatter:function(rowIndex, rowData){
            return '<div style="padding:5px"><table class="task" id="taskList_'+rowData.id+'" ></table></div>';
        },

        onExpandRow: function(index,row){
            var $dataList=$('#dataList');
            var rows=$dataList.datagrid('getRows');
            for(var i=0;i<rows.length;i++){
                var rowIndex=$dataList.datagrid('getRowIndex',rows[i]);
                if(index !== rowIndex){
                    $dataList.datagrid('collapseRow',rowIndex);
                }
            }
            lastIndex=index;
            $('#taskList_'+row.id).datagrid({
                url: ROOT_PATH + '/task/selectByTaskId.shtml',
                queryParams: {
                    taskId: row.id
                },
                fitColumns: true,
                singleSelect: true,
                rownumbers: true,
                loadMsg: '',
                height: 'auto',
                columns: [[
                    {
                        field: 'id',
                        checkbox: true
                    },
                    {field: 'taskName', title: '任务名称', width: 120},
                    {field: 'taskDescribe', title: '任务描述', width: 80},
                    {field: 'receiveUserName', title: '接收人', width: 100},
                    {field: 'jsDeptName', title: '接收单位', width: 100},
                    {field: 'requireTime', title: '要求完成日期', width: 100},
                    {field: 'acceptUserName', title: '验收人', width: 100},
                    {field: 'ysDeptName', title: '验收单位', width: 100},
                    {field: 'addTime', title: '下发时间', width: 100},
                    {field: 'state', title: '状态', width: 100,formatter: function (value, row, index) {
                        return typeof (row.state)==='undefined'?'<span class="color-important text-white" style="padding: 3px;border-radius:5px;">未下发</span>':(row.state=='1'?'<span class="color-info text-white" style="padding: 3px;border-radius:5px;">整改中</span>':'<span class="color-success text-white" style="padding: 3px;border-radius:5px;">已验收</span>');
                    }}
                ]],
                onLoadSuccess: function () {
                    setTimeout(function () {
                        $('#dataList').datagrid('fixDetailRowHeight', index);
                    }, 0);
                }
            });
            $('#dataList').datagrid('fixDetailRowHeight', index);
        }

    });


}




$(function () {
    loadDataList();

    //高级查询
    initAdvancedSearch();

    //查询按钮事件
    initSearchClick();

    //重置参数
    initResetClick();


    //任务下发按钮事件
    $('#btn_monitor_rectification_issued').on('click',function () {

        var rows=$('#dataList').datagrid('getChecked');
        if(rows.length === 0){
            layer.alert("选择需要修改的行！");
            return
        }

        var objTask={};//参数对象
        var taskArr=[];//任务数据数组
        for (var i=0;i<rows.length;i++){
            var obj={
                taskId:rows[i].id,
                taskName:rows[i].monitorPointName+"->"+rows[i].monitorProjectName+'异常整改',//任务名称
                taskSource:'监测点异常整改'//模块
            };
            taskArr.push(obj);
        }
        objTask.task=taskArr;
        WindowManager.openLayer({//任务窗口
            type: 2,
            title: '批量任务下发',
            area: ['1150px', '70%'],
            content: ROOT_PATH + '/task/addTask.shtml',
            data:objTask,//参数
            callBack:function(data){//窗口关闭后的回调函数 data=true
                if (data){
                    $('#dataList').datagrid('reload');//重新加载表格
                }
            }
        });
    });
    //撤销任务
    $('#btn_monitor_rectification_revoke').on('click',function () {
        if(lastIndex!==undefined){
            var $Expander=$('#dataList').datagrid('getRowDetail',lastIndex).find('.task');
            //alert($Expander.id)
            var rows = $Expander.datagrid('getSelections');
            if(rows.length>0){
                if(rows[0].state=='0'){
                    layer.alert("该任务已验收！");
                    return;
                }
                var id=rows[0].id;
                layer.confirm('是否确定撤销当前任务？', {
                    btn: ['确定', '取消'] //按钮
                }, function () {
                    $.ajax({
                        type: 'post',
                        url: ROOT_PATH + '/task/deleteById.shtml',
                        data: {id: id},
                        dataType: "json",
                        success: function (result) {
                            if (result.success) {
                                $('#dataList').datagrid('reload');//重新加载表格
                                layer.msg('撤销成功', {
                                    icon: 1,
                                    time: 1000
                                });
                            }else {
                                layer.msg('撤销失败', {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    });
                });
            }else{
                layer.alert("请选择撤销的任务");
            }

        }else{
            layer.alert("请选择撤销的任务");
        }

    })




});
function showDetails(id) {

    openLayer("实验室监测数据登记详情",ROOT_PATH+'/environmental/monitor/registration/details.shtml?id='+id);

}